(function($){
	
	$(document)
	.ready(function(){
		jQselectList = $('#selectList');
		jQimageList = $('#imageList');

		jQloaderAnimation.ajaxStart(function(){jQloaderAnimation.show();});
		jQloaderAnimation.ajaxStop(function(){jQloaderAnimation.hide();});
		
		jQimageList.sortable({ placeholder: "hover"});
		jQimageList.children('li')
		.dblclick(function(){
			var $this = $(this);
			if($this.parent('#selectList').size() != 0){
				$this.appendTo(jQimageList);
				jQimageList.sortable({ placeholder: "hover"});
			}
			else{
				$this.appendTo(jQselectList);
			};
			jQselectList.selectable({filter:'li',distance:1,autoRefresh:false});
		})
		.click(function(){$(this).toggleClass("ui-selected");});


		/**
		 *	Set the width and height of all the form parts
		 */
		var px_childCnt = $('.px_child').css({width:970,float:'left'}).size();

		/**
		 *	Set the width of the form parts holder
		 */
		$('#px_gut').width(px_childCnt*970);

		$('a.goBack').click(function(){
			var $this = $(this);
			var goToWhere = $(this).attr('name');
	
			var goToSpot = $('#'+goToWhere).position();
			goToSpot = (goToSpot.left*-1) + parseInt($('#px_gut').css('marginLeft'));
	
			animateMe('#px_gut',goToSpot,goToWhere);
			return false;
		});
		
		$('input[name=px_goNext]').click(function(){
			var goToWhere = $(this).attr('title');
			var commingFrom = $('input[name=px_currentScreen]').val();
			var errors = Array();
			if(goToWhere == 'px_picasaThumbnails'){
				if(commingFrom == 'px_picasaBasic'){
					var url = $('input[name=px_picasaAlbumUrl]').val();
					if(isEmpty(url)){
						showNoticeMessage('error','<?php _e('You need to provide a URL for your Picasa URL', 'photoxhibit'); ?>');
						return false;
					}
					else if(!/picasa/.test(url)){
						showNoticeMessage('error','<?php _e('The URL provided is not to Picasa', 'photoxhibit'); ?>');
						return false;
					}
					else if(!/rss/.test(url)){
						showNoticeMessage('error','<?php _e('The URL provided is not a RSS feed to Picasa', 'photoxhibit'); ?>');
						return false;
					};
					$('.px_picasaTn').attr('name', 'px_picasaBasic');
				}
				else if(commingFrom == 'px_picasaAdvance'){
					if(isEmpty($('input[name=picasa_user_id]').val())){
						errors.push('<?php _e('Please Enter a Picasa User ID', 'photoxhibit'); ?>');	
					};
					if(isEmpty($('input[name=picasa_album_id]').val())){
						errors.push('<?php _e('Please Enter a Picasa Album ID', 'photoxhibit'); ?>');	
					};
				};
			}
			else if(goToWhere == 'px_buildFlickrThumbnails'){
				if(commingFrom == 'px_buildFlickrBasic'){
					var url = $('input[name=px_flickr_basic_url]').val();
					
					if(isEmpty(url)){
						showNoticeMessage('error','<?php _e('You need to provide a URL for your Flickr URL', 'photoxhibit'); ?>');
						return false;
					}
					else if(!/flickr/.test(url)){
						showNoticeMessage('error','<?php _e('The URL provided is not to Flickr', 'photoxhibit'); ?>');
						return false;
					}
					else if(!/rss/.test(url) && !/feeds/.test(url)){
						showNoticeMessage('error','<?php _e('The URL provided is not a RSS feed to Flickr', 'photoxhibit'); ?>');
						return false;
					};
				}
				else if(commingFrom == 'px_buildFlickrAPI'){
					if(isEmpty($('input[name=flickr_api_key]').val())){
						errors.push('<?php _e('Please Enter a Flickr API Key', 'photoxhibit'); ?>');	
					};
				};			
			}
			else if(goToWhere == 'px_buildSmugMugThumbnails'){
				if(commingFrom == 'px_buildSmugMugBasic'){
					var url = $('input[name=px_smugmug_basic_url]').val();
					if(isEmpty(url)){
						showNoticeMessage('<?php _e('error','You need to provide a URL for your SmugMug URL', 'photoxhibit'); ?>');
						return false;
					}
					else if(!/smugmug/.test(url) && !/feed/.test(url)){
						showNoticeMessage('error','<?php _e('The URL provided is not to SmugMug', 'photoxhibit'); ?>');
						return false;
					}
					else if(!/rss/.test(url) && !/feed/.test(url)){
						showNoticeMessage('error','<?php _e('The URL provided is not a RSS feed to SmugMug', 'photoxhibit'); ?>');
						return false;
					};
				}
				else if(commingFrom == 'px_buildSmugMugAdvance'){
					/*
					if(isEmpty($('input[name=picasa_user_id]').val())){
						errors.push('Please Enter a Picasa User ID');	
					}
					if(isEmpty($('input[name=picasa_album_id]').val())){
						errors.push('Please Enter a Picasa Album ID');	
					}
					*/
				};
			}
			else if(goToWhere == 'px_buildFlickrAPI'){
				if(commingFrom == 'px_buildFlickrPhotoset'){
					if(isEmpty($('input[name=flickr_photoset_id]').val())){
						errors.push('<?php _e('Please Enter a Flickr Photoset ID', 'photoxhibit'); ?>');	
					};				
				}
				else if(commingFrom == 'px_buildFlickrSearch'){
					if(isEmpty($('input[name=flickr_user_id]').val()) && isEmpty($('input[name=flickr_group_id]').val()) && isEmpty($('input[name=flickr_tags]').val())){
						errors.push('<?php _e('Please enter something in either "Flickr User ID", "Flickr Group ID", or "Flickr Tags" otherwise use the basic option', 'photoxhibit'); ?>');	
					};				
				}
			}
			else if(goToWhere == 'px_buildAlbumThumbnails'){
				if( $('#px_album_list_sm :selected').val() == 0 ){
					errors.push('<?php _e('Please Select an Album', 'photoxhibit'); ?>');	
				};
			};
			//
			switch(goToWhere){
				case 'px_buildSmugMugAdvance':
					switch(commingFrom){
						case 'px_buildSmugMugAPI':
							if(isEmpty($('input[name=smugmug_api_key]').val())){
								errors.push('<?php _e('Please enter a SmugMug API Key', 'photoxhibit'); ?>');	
							};
							break;
					};
					break;
				case 'px_buildSmugMugThumbnails':
					switch(commingFrom){
						case 'px_buildSmugMugAdvance':
							if(isEmpty($('input[name=smugmug_user_id]').val())){
								errors.push('<?php _e('Please enter a SmugMug User ID', 'photoxhibit'); ?>');	
							};
							if(isEmpty($('input[name=smugmug_album_id]').val())){
								errors.push('<?php _e('Please enter a SmugMug Album ID', 'photoxhibit'); ?>');	
							};
							break;
					};
					break;
			};
			
			if(errors.length > 0){
				var i = 0;
				var msg = '';
				for(i = 0; i < errors.length; i++){
					msg += errors[i] +"<br/>";
				};
				showNoticeMessage('error',msg);
				return false;
			};
			
			if($(this).hasClass('px_pxOptAdv')){
				$('.px_picasaTn').attr('name','px_picasaAdvance');
			}		
			else if($(this).hasClass('px_pxOptBasic')){
				$('.px_picasaTn').attr('name','px_picasaBasic');
			}		
			else if($(this).hasClass('px_pxFlickrOptBasic')){
				$('.px_flickrTn').attr('name','px_buildFlickrBasic');
			}
			else if($(this).hasClass('px_flickrPSApi')){
				$('.px_flickrTn').attr('name','px_buildFlickrPhotoset');
			}
			else if($(this).hasClass('px_flickrSApi')){
				$('.px_flickrTn').attr('name','px_buildFlickrSearch');
			}
			else if($(this).hasClass('px_flickrApiToTn')){
				$('.px_flickrTn').attr('name','px_buildFlickrAPI');
			}
			else if($(this).hasClass('px_pxSmugMugOptBasic')){
				$('.px_smugMugTn').attr('name','px_buildSmugMugBasic');
			}
			else if($(this).hasClass('px_buildSmugMugAdvance')){
				$('.px_smugMugTn').attr('name','px_buildSmugMugAdvance');
			};
			
			switch(goToWhere){
				case 'px_go':
					var jqcheckedservice = $('input[name=service]:checked');
					if(jqcheckedservice.size()==0){
						showNoticeMessage('error',"<?php _e('Please select a service', 'photoxhibit'); ?>");
						return false;
					};
					$('input[name=px_serviceHidden]').val(jqcheckedservice.val());
					goToWhere = jqcheckedservice.attr('alt');
					break;
				case 'px_picasaOpt':
					var jqselectVal = $('#px_picasaOptionsDD :selected').val();
					if(isEmpty(jqselectVal)){
						showNoticeMessage('error',"<?php _e('Please select a option', 'photoxhibit'); ?>");
						return false;
					};
					goToWhere = jqselectVal;
					break;
				case 'px_flickrOpt':
					var jqselectVal = $('#px_flickrOptionsDD :selected').val();
					if(isEmpty(jqselectVal)){
						showNoticeMessage('error',"<?php _e('Please select a option', 'photoxhibit'); ?>");
						return false;
					};
					goToWhere = jqselectVal;
					break;
				case 'px_smugMugOpt':
					var jqselectVal = $('#px_smugMugOptionsDD :selected').val();
					if(isEmpty(jqselectVal)){
						showNoticeMessage('error',"<?php _e('Please select a option', 'photoxhibit'); ?>");
						return false;
					};
					goToWhere = jqselectVal;
					break;
			};
			
			var goToSpot = $('#'+goToWhere).position();
			goToSpot.left += (parseInt($('#px_gut').css('marginLeft'))*-1);
			animateMe('#px_gut',-goToSpot.left,goToWhere);
			
			
			return false;
		});

		$('.px_getPhotos').click(function(){
			var url = null, error = Array(), service = $('input[name=px_serviceHidden]').val(), option = '';
			var getData = {}, dataType = 'jsonp'
			
			switch(service){
				case 'picasa':
					option = $('#px_picasaOptionsDD :selected').val();
					switch(option){
						case 'px_picasaBasic':
							url = $('input[name=px_picasaAlbumUrl]').val();
							if(isEmpty(url)){
								error.push('<?php _e('Some how you got this far without providing a Picasa URL, provide a URL for your Picasa URL', 'photoxhibit'); ?>');
							};
							
							url = url.replace(/alt=rss/g,"alt=json");
							
							break;
						case 'px_picasaAdvance':
							var uid = $('#picasa_user_id').val();
							if(uid == ''){
								error.push('<?php _e('Some how you got this far without providing a picasa user id, please provide one', 'photoxhibit'); ?>');
							};
							var aid = $('#picasa_album_id').val();
							if(aid == ''){
								error.push('<?php _e('Some how you got this far without providing a picasa album id, please provide one', 'photoxhibit'); ?>');
							};
							url  = "http://picasaweb.google.com/data/feed/api/user/"+uid+"/album/"+aid+"?kind=photo&alt=json";
							break;
						default:
							error.push('<?php _e('For some reason the Picasa options select menu was just used, go back and select basic or advanced', 'photoxhibit'); ?>');
					};
					break;
				case 'flickr':
					option = $('#px_flickrOptionsDD :selected').val();
					switch(option){
						case 'px_buildFlickrBasic':
							url = $('input[name=px_flickr_basic_url]').val();
							if(isEmpty(url)){
								error.push('<?php _e('Some how you got this far without providing a Flickr URL, provide a URL for your Flickr URL', 'photoxhibit'); ?>');
							};
							url +="&format=json&jsoncallback=?"
							break;
						case 'px_buildFlickrPhotoset':
							var pid = $('#flickr_photoset_id').val();
							url = baseUrl + "flickrAction";
							getData.pxsecondary = 'grabImages';
							getData.photosetId = pid;
							dataType = 'json';
							
							if(pid == ''){
								error.push('<?php _e('You do need a photoset id if you a going to pull in a Photoset', 'photoxhibit'); ?>');
							};
						
							break;
						case 'px_buildFlickrSearch':
							var pid = $('#flickr_user_id').val();
							url = baseUrl + "flickrAction";
							getData.pxsecondary = 'grabSearch';
							getData.user_id = pid;
							getData.tags = $('#flickr_tags').val();
							getData.tag_mode = $('#flickr_tag_mode option:selected').val();
							getData.group_id = $('#flickr_group_id').val();
							getData.sortby = $('#flickr_sort').val();
							dataType = 'json';
	
							if	(
									getData.user_id == '' &&
									getData.group_id == '' &&
									getData.tags == ''
								){
								getData.method = "recent";
							}
							else{
								getData.method = "search";
							};

							break;
						default:
							error.push('<?php _e('For some reason the Flickr options select menu was just used, go back and select something', 'photoxhibit'); ?>');
					};
					break;
				case 'smugmug':
					url = baseUrl + "smugmugAction";
					getData.pxsecondary = 'grabImages';
					getData.albumId = $('input[name=smugmug_album_id]').val();
					getData.albumKey = $('input[name=smugmug_album_id]').attr('rel');
					dataType = 'json';
					break;
				case 'album':
					url = baseUrl + '?option=getPhotos&service=album&url='+$('#px_album_list_sm :selected').val();
					break;
				case 'locally':
					url = baseUrl + "localAction";
					dataType = 'json';
					break;
				case 'browse':
					url = baseUrl + '?option=getPhotos&service=browse&url='+$('#browseBasic_url').val();
					break;
				default:
					return false;			
			};
			
			if(url == '' || url === undefined || url == null || isEmpty(url)){
				error.push('<?php _e('Need that url.  Go ahead, add it.  I need it.', 'photoxhibit'); ?>');
			};
			
			if(error.length > 0){
				var i = 0;
				var msg = '';
				for(i = 0; i < error.length; i++){
					msg += error[i] +"<br/>";
				};
				showNoticeMessage('error',msg);
				return false;
			};
	
			showNoticeMessage('updated','<?php _e('Loading your images, please wait...', 'photoxhibit'); ?>');


			if(service == 'browsable'){
				//getImagesFromDir(url);
			}
			else{
	
				$.ajax({
					url: url,
					dataType: dataType,
					data : getData,
					cache:true,
					success : function(data, textStatus){
						with(data){
							//smugmug error controls
							if(typeof code != 'undefined'){
								if(code == 15 || code == 4 || code == 5){
									showNoticeMessage('error','SmugMug sent back a stat of "'+stat+'" with a message of "'+message+'"');
									return false;
								}
								else if(code == 2){
									showNoticeMessage('error','Flickr sent back a stat of "'+stat+'" with a message of "'+message+'"');
									return false;
								};
							}
							else if(typeof result != 'undefined'){
								if(result == 'error'){
									showNoticeMessage('error','<?php _e('There was an error processing that url.', 'photoxhibit'); ?>');
									return false;
								}
								else if(result == 'noimage'){
									showNoticeMessage('error','<?php _e('There was no images in the album you selected.', 'photoxhibit'); ?>');
									return false;
								};
							};
						};

						processData(data, service);
					},
					error : function(x, txt, e){
						showNoticeMessage('error','<?php _e('There seemed to be an error in the execution of the URL provided; please select another one and try again.  If problem presists, please submit a bug.', 'photoxhibit'); ?>');
					}
				});
			};
	
			return false;
		});	//	end : $('.px_getPhotos').click


		processData = function(data, service){
			showNoticeMessage('updated','<?php _e('Ok, got the data, going to process it now.', 'photoxhibit'); ?>');
			imageGroup = Array();
			var tmpArray = Object();
			//picasa flickr smugmug  album locally browse
			switch(service){
				case 'picasa':
					var obj = data['feed']['entry'];
					$.each(obj,function(i){
						if($('select[name=px_picasaThumbnailSelect] :selected').val() > 2){
							var tn = obj[i]['media$group']['media$content'][0]['url']+'?imgmax='+$('select[name=px_picasaThumbnailSelect] :selected').val()
						}
						else{
							var tn = obj[i]['media$group']['media$thumbnail'][$('select[name=px_picasaThumbnailSelect] :selected').val()]['url']
						};
	
						tmpArray = {
							"d"	: tn,
							"t"	: tn,
							"f"	: obj[i]['media$group']['media$content'][0]['url']+'?imgmax='+$('select[name=px_picasaFullSizeSelect] :selected').val(),
							"a"	: obj[i]['media$group']['media$description']['$t'],
							"i"	: 0
						};
						imageGroup.push(tmpArray);
					});
					break;
				case 'flickr':
					var tn = Array('_s','_t','_m','','_b','_o');
					var tnSize = $('#px_flickr_thumbnailSelect option:selected').val();
					var full = Array('_s','_t','_m','','_b','_o');
					var fullSize = $('#px_flickr_largeSelect option:selected').val();

					switch($('#px_flickrOptionsDD :selected').val()){
						case 'px_buildFlickrBasic':
							obj = data['items'];
					
							$.each(obj, function(i){
								tmpArray = {
									"d"	: obj[i]['media']['m'].replace(/_m.jpg/, '_s.jpg'),
									"t"	: obj[i]['media']['m'].replace(/_m.jpg/, tn[tnSize]+'.jpg'),
									"f"	: obj[i]['media']['m'].replace(/_m.jpg/, tn[fullSize]+'.jpg'),
									"a"	: obj[i]['title'],
									"i"	: 0
								};
								
								imageGroup.push(tmpArray);
							});
							break;
						case 'px_buildFlickrPhotoset':
						case 'px_buildFlickrSearch':

							with( data ){
								for( i in photo ){
									var base = 'http://farm'+photo[i]['farm']+'.static.flickr.com/';
									base += photo[i]['server']+'/'+photo[i]['id']+'_';
									
									var t = base + photo[i]['secret']+tn[tnSize]+'.jpg';
									var f = base + photo[i]['secret']+tn[fullSize]+'.jpg';
									
									tmpArray = {
										"d" : base + photo[i]['secret']+'_s.jpg',
										"t"	: t,
										"f"	: f,
										"a"	: photo[i]['title'],
										"i"	: 0
									};
									imageGroup.push(tmpArray);
								};
							};
							break;
					};
					break;
				case 'smugmug':
					var tnSize = $('#px_smugMugThumbnailSelect :selected').val();
					var fullSize = $('select[name=px_smugMugFullSizeSelect] :selected').val();
					
					var tn = Array('TinyURL','ThumbURL','SmallURL', 'MediumURL');
					var l = Array('','','','MediumURL','LargeURL','XLargeURL');

					$.each(data,function(i){
						tmpArray = {
							'd' : data[i]['TinyURL'],
							"t"	: data[i][tn[tnSize]],
							"f"	: data[i][l[fullSize]],
							"a"	: data[i]['Caption'],
							"i"	: 0
						};
						imageGroup.push(tmpArray);
					});

					break;
				case 'smugmugA':
					var tnSize = $('#px_smugMugThumbnailSelect :selected').val();
					var fullSize = $('select[name=px_smugMugFullSizeSelect] :selected').val();
		
					$.each(data.Images,function(i){
						tmpArray = {
							'd' : data.Images[i]['TinyURL'],
							"t"	: data.Images[i][tn[tnSize]],
							"f"	: data.Images[i][l[fullSize]],
							"a"	: data.Images[i]['Caption'],
							"i"	: 0
						};
						imageGroup.push(tmpArray);
					});
					break;
				case 'locally':
					for( i in data ){
						tmpArray = {
							'd' : data[i]['thumbnail'][0],
							"t"	: data[i]['intermediate'][0],
							"f"	: data[i]['url'],
							"a"	: data[i]['post_excerpt'],
							"i"	: 0
						};
						imageGroup.push(tmpArray);
					};
					break;
				case 'browse':
					break;
				case 'album':
					var tnarray = Array('_tn','_ltn','');
					var tn = $('select[name=px_albumThumbnailSelect] :selected').val();
					var path = $('#px_album_list_sm :selected').attr('meta');
					$.each(data,function(i){
						data[i]['albumPhotos_alt'] = (data[i]['albumPhotos_alt'] == 'null') ? '' : data[i]['albumPhotos_alt'];
						tmpArray = {
							"t"	: path + '/' + data[i]['albumPhotos_file'] + tnarray[tn] + '.' + data[i]['albumPhotos_ext'],
							"f"	: path + '/' + data[i]['albumPhotos_file'] + '.' + data[i]['albumPhotos_ext'],
							"a"	: data[i]['albumPhotos_alt'],
							"i"	: data[i]['albumPhotos_id']
						};
						imageGroup.push(tmpArray);
					});
					break;
				default:
			};

			processImageArray();
		};
	
		processImageArray = function(){
			showNoticeMessage('updated','<?php _e('Done processing, now going to push out the images.', 'photoxhibit'); ?>');

			jQselectList.empty().selectable('destroy');

			var l = imageGroup.length;

			for( var i = 0; i < l; i++ ){
				
				var meta = "{";
				meta += '"t":"'+imageGroup[i]['t']+'",';
				meta += '"f":"'+imageGroup[i]['f']+'",';
				meta += '"i":"'+imageGroup[i]['i']+'",';
				meta += '"a":"'+imageGroup[i]['a']+'"';
				meta += "}";
				
				//var image = $('<img src="'+imageGroup[i]['d']+'"/>').data('metadata',meta);
				var image = $('<img src="'+imageGroup[i]['d']+'"/>').attr('metadata',meta);
				var $img = $('<li>').append(image)
				.dblclick(function(){
					var $this = $(this);
					if($this.parent('#selectList').size() != 0){
						$this.appendTo(jQimageList);
						jQimageList.sortable({ placeholder: "hover"});
					}
					else{
						$this.appendTo(jQselectList);
					};
				})
				.click(function(){$(this).toggleClass("ui-selected");})
				.appendTo(jQselectList);
				
				jQselectList.selectable({filter:'li',distance:1,autoRefresh:false});
			};
			
			showNoticeMessage('updated','<?php _e('Done pushing out the images.', 'photoxhibit'); ?>');
			
			var t = pxjQuery('#px_table').offset().top;

			$('html,body').animate({scrollTop: t}, 1000);
		};

		$('#px_add_all').click(function(){
			jQselectList.children('li')
			.removeClass('ui-selected')
			.appendTo(jQimageList);
			jQimageList.sortable({ placeholder: "hover"});
			return false;
		});

		$('#px_add_all_selected').click(function(){
			jQselectList.children('li.ui-selected')
			.removeClass('ui-selected')
			.appendTo(jQimageList);
			jQimageList.sortable({ placeholder: "hover"});
			return false;
		});

		$('#px_remove_all').click(function(){
			jQimageList.children('li')
			.appendTo(jQselectList)
			.removeClass('ui-selected');
			jQselectList.selectable({filter:'li',distance:1,autoRefresh:false});
			return false;
		});

		$('#px_remove_all_selected').click(function(){
			jQimageList.children('li.ui-selected')
			.appendTo(jQselectList).removeClass('ui-selected');
			jQselectList.selectable({filter:'li',distance:1,autoRefresh:false});
			return false;
		});

		$('#px_selectPlugin').change(function(){
			var $this = $(':selected',this);
			var url = $this.attr('title');
			$('#params').empty();
			var params = decodeURIComponent($this.attr('metadata'));
			
			if( !params ) return;
			
			params = JSON.parse(params);

			$('#selectGalleryUrl').html('View an example at <a href="'+url+'" target="_blank">'+url+'</a>');
				
			if( typeof params == "undefined" ) return false;

			$.each(params.parameters,function(i){
				var $tr = $('<tr>');
				$td = $('<td>').appendTo($tr).text(params.parameters[i].param);
				$td = $('<td>').appendTo($tr).html('<input type="text" size="30" name="'+params.parameters[i].param+'"/>');
				$td = $('<td>').appendTo($tr).text(params.parameters[i].desc);
				$('#params').append($tr);
			});			
		}); // end : $selectGallery.change(function(){
		


		/**
		 *	Here we are setting the change event to the smugmug stored user ids
		 *	select menu and append that value to the smugmug userid input field
		 */
		$('#smugmug_user_id_dd').change(function(){
			var $this = $(':selected',this);
			$('#smugmug_user_id').empty().val($this.val());
			$('#px_smugmugAlbum_grab').trigger('click');
		});  //  end : $smugmug_user_id_dd.change
		
	
		/**
		 *	
		 *	
		 */
		$('#px_smugmugAlbum_grab').click(function(){
			var uid = $('#smugmug_user_id').val();
			var api = "<?php echo $this->smugmugapi;?>";

			var un = $('#smugmug_user_id').val();
			if(uid == ''){
				showNoticeMessage('error','Need a smugmug user id');
				return false;
			};

			showNoticeMessage('updated','<?php _e('Grabbing your album list from SmugMug, please wait', 'photoxhibit'); ?>');
			$('#px_smugmugAlbum_grab_notice').text('Grabbing your album list from SmugMug, please wait');
			
			$.ajax({
				url : baseUrl + 'smugmugAction',
				dataType : 'json',
				data : {un:un,pxsecondary:'grabAlbum'},
				success : function(Albums){
					
					showNoticeMessage('updated','<?php _e('Ok, got them, now building the dropdown.', 'photoxhibit'); ?>');
					
					$('#px_smugmugAlbum_grab_notice').text('Ok, got them, now building the dropdown.');

					$('#smugmug_album_id_dd').empty().append('<option selected="selected">Select one</option>');
					
					var l = Albums.length;
					
					for(var i = 0; i < l; i++){
						$('#smugmug_album_id_dd')
							.append('<option value="'+Albums[i]['id']+'" rel="'+Albums[i]['Key']+'">'+Albums[i]['Title']+' [Image Count: ' + Albums[i]['ImageCount'] + ']</option>');
					};
					
					$('#smugmug_album_id_dd').append('<option selected="selected">Select one</option>');
					
					showNoticeMessage('updated','<?php _e('Ok, Done!  Just to note, depending on the browser, the last one added may be the one that gets selected.  So be sure to select the one you want.', 'photoxhibit'); ?>');

					$('#px_smugmugAlbum_grab_notice')
					.text('Ok, Done!  Just to note, depending on the browser, the last one added may be the one that gets selected.  So be sure to select the one you want.');

				},
				error : function(){}
			});

			return false;
		});  //  end : $('#px_smugmugAlbum_grab').click

		/**
		 *	
		 */
		$('#smugmug_album_id_dd').change(function(){
			var $this = $(':selected',this);
			$('#smugmug_album_id').empty().val($this.val());
			$('#smugmug_album_id').removeAttr('rel').attr('rel',$this.attr('rel'));
		});  //  end : $smugmug_album_id_dd.change
		


		/**
		 *	Here we are setting the change event to the smugmug stored user ids
		 *	select menu and append that value to the smugmug userid input field
		 */
		$('#picasa_user_id_dd').change(function(){
			var $this = $(':selected',this);
			$('#picasa_user_id').empty().val($this.val());
			$('#px_picasa_grab').trigger('click');
		});  //  end : $smugmug_user_id_dd.change
		
	
		/**
		 *	
		 *	
		 */
		$('#px_picasa_grab').click(function(){
			var uid = $('#picasa_user_id').val();

			if(uid == ''){
				showNoticeMessage('error','Need a picasa user id');
				return false;
			};
			
			showNoticeMessage('updated','<?php _e('Grabbing your album list from Picasa, please wait', 'photoxhibit'); ?>');
			$('#px_picasa_grab_notice').text('<?php _e('Grabbing your album list from Picasa, please wait', 'photoxhibit'); ?>');
			
			$.ajax({
				url : baseUrl + 'picasaAction',
				dataType : 'json',
				data : {un:uid,pxsecondary:'grabAlbum'},
				success : function(Albums){
	
					showNoticeMessage('updated','<?php _e('Ok, got them, now building the dropdown.', 'photoxhibit'); ?>');
					
					$('#px_picasa_grab_notice').text('<?php _e('Ok, got them, now building the dropdown.', 'photoxhibit'); ?>');

					$('#picasa_album_id_dd').empty().append('<option selected="selected"><?php _e('Select one', 'photoxhibit'); ?></option>');
					with( Albums.feed ){
						var l = entry.length;
						for(var i = 0; i < l; i++){
							var txt = entry[i]['gphoto$name']['$t']
							txt += ' [Image Count: ' + entry[i]['gphoto$numphotos']['$t'] + ']';

							$('#picasa_album_id_dd')
								.append('<option value="'+entry[i]['gphoto$id']['$t']+'" rel="">'+txt+'</option>');
						};
					};

					$('#picasa_album_id_dd')
						.append('<option selected="selected"><?php _e('Select one', 'photoxhibit'); ?></option>');
						
					showNoticeMessage('updated','<?php _e('Ok, Done!  Just to note, depending on the browser, the last one added may be the one that gets selected.  So be sure to select the one you want.', 'photoxhibit'); ?>');
						
					$('#px_picasa_grab_notice')
						.text('<?php _e('Ok, Done!  Just to note, depending on the browser, the last one added may be the one that gets selected.  So be sure to select the one you want.', 'photoxhibit'); ?>');

				},
				error : function(){}
			});

			return false;
		});  //  end : $('#px_smugmugAlbum_grab').click

		/**
		 *	
		 */
		$('#picasa_album_id_dd').change(function(){
			var $this = $(':selected',this);
			$('#picasa_album_id').empty().val($this.val());
			$('#picasa_album_id').removeAttr('rel').attr('rel',$this.attr('rel'));
		});  //  end : $smugmug_album_id_dd.change
		
		$('#flickr_user_id_dd').change(function(){
			var $this = $(':selected',this);
			$('#flickr_user_id').empty().val($this.val());
		});  //  end : $smugmug_album_id_dd.change
		
		$('#flickr_ps_user_id_dd').change(function(){
			var $this = $(':selected',this);
			$('#flickr_ps_user_id').empty().val($this.val());
			$('#px_flickr_grab_ps').trigger('click');
		});  //  end : $smugmug_album_id_dd.change
		
		$('#px_flickr_grab_ps')
		.click(function(){
			var uid = $('#flickr_ps_user_id').val();
			if( uid == '' ){
				showNoticeMessage('error','<?php _e('Need a Flickr user id', 'photoxhibit'); ?>');
				return false;
			};
			
			showNoticeMessage('notice','<?php _e('Grabbing your photoset list from Flickr, please wait', 'photoxhibit'); ?>');
			
			$.ajax({
				url : baseUrl + 'flickrAction',
				dataType : 'json',
				data : {un:uid,pxsecondary:'grabPhotosets'},
				success : function(photosets){
					showNoticeMessage('notice','<?php _e('Ok, got them, now building the dropdown.', 'photoxhibit'); ?>');

					$('#flickr_ps_photoset_id_dd')
					.empty()
					.append('<option selected="selected"><?php _e('Select one', 'photoxhibit'); ?></option>');
			
					with(photosets){
						var l = photoset.length;
						for(var i = 0; i < l; i++){
							var txt = photoset[i]['title']+' [Image Count: ' + photoset[i]['photos'] + ']';
							$('#flickr_ps_photoset_id_dd')
								.append('<option value="'+photoset[i]['id']+'" rel="">'+txt+'</option>');
						};
					};
					$('#flickr_ps_photoset_id_dd').append('<option selected="selected"><?php _e('Select one', 'photoxhibit'); ?></option>');
					
					showNoticeMessage('notice','<?php _e('Ok, Done!  Just to note, depending on the browser, the last one added may be the one that gets selected.  So be sure to select the one you want.', 'photoxhibit'); ?>');
				},
				error : function(){}
			});
				
			return false;				
		});
		
		$('#flickr_photoset_id_dd, #flickr_ps_photoset_id_dd')
		.change(function(){
			var $this = $(':selected',this);
			$('#flickr_photoset_id').empty().val($this.val());
		}); 
		

	
	
		$('#px_buildSubmit').click(function(){
			//get all images in image list
			var jQimg = $('#imageList img');
			
			var gid = $('#px_gidHidden').val();
			var curplugin_id = $('#px_pluginid').val();
			
			var pluginId = $('#px_selectPlugin :selected').val();
			var galleryName = $('input[name=px_galleryName]').val();
			//var sendData = "option=processGallery&gallery_id="+gid;
			
			var sendData = {gallery_id:gid};
			
			var error = Array();
			var imageArray = Array();
			var dataStructure = $('input[name=px_structure]:checked').val();
	
			// start : check for make sure the important stuff is done
			if(jQimg.size() < 2){
				error.push('<?php _e('Really?  You want an image/photo gallery with less then two image?  How about you add one or two more?', 'photoxhibit'); ?>');
			};
			if(pluginId==0){
				error.push('<?php _e('Ok, you are trying to build a gallery for your blog, so selecting a gallery plugin from the "Select the gallery style" select menu will help you get to that point.', 'photoxhibit'); ?>');
			};
			if(galleryName==''){
				error.push('<?php _e('Naming the gallery will make things easier for you later on, so do yourself a favor and put some text in the "Name your gallery" field', 'photoxhibit'); ?>');
			};
			if(dataStructure==''){
				error.push('<?php _e('Please select a structure you want.', 'photoxhibit'); ?>');
			};
			
			if(error.length > 0){
				var i = 0;
				var msg = '';
				for(i = 0; i < error.length; i++){
					msg += error[i] +"<br/>";
				};
				showNoticeMessage('error',msg);
				return false;
			};
			// lets disable the set gallery button just to make sure it is not clicked again.
			//$(this).attr('disabled',true);
	
			//  Lets run thru all the images with have and put them in an array
			jQimg.each(function(i){
				//imageArray.push($(this).data('metadata'));
				imageArray.push( JSON.stringify( $(this).metadata({type:'attr',name:'metadata'}) ) );
			});
	
			// build our sendData string for sending to the server
			sendData.images = '['+imageArray.join(",")+']'
			//console.log(sendData);
			
			sendData.gallery_title = encodeURIComponent(galleryName);
			sendData.plugin_id = pluginId;
			sendData.curplugin_id = curplugin_id;
			sendData.gallery_structure = dataStructure;
			
			if($('input[name=px_album_uselarge]').is(':checked')){
				sendData.gallery_uselarge = '1';
			}
			else{
				sendData.gallery_uselarge = '0';
			};
			
			if($('input[name=px_structure_tableVar'+dataStructure+']').size() != 0){
				sendData.gallery_extra = $('input[name=px_structure_tableVar'+dataStructure+']').val();
			}
			else{
				sendData.gallery_extra = null;
			};
			
			var paramArray = Array();
			var tmpArray = Array();

			$('#params input').each(function(){
				var $this = $(this);
				var val = $this.val();
				if(val !=''){
					if(val.match(/\{(.*)\}/)){//check to see if it is an object format
						tmpArray.push(''+$this.attr('name')+''+':'+val+'');
					}
					else if(isInteger(val)){
						tmpArray.push(''+$this.attr('name')+''+':'+val+'');
					}
					else{
						tmpArray.push(''+$this.attr('name')+''+':"'+val+'"');
						val = '"'+val+'"';
					};
					paramArray.push('"'+$this.attr('name')+'"'+':'+val+'');
				};
			});
	
			// finish building out our sendData variable
			sendData.gallery_params = '{'+paramArray.join(",")+'}';
	
			// lets block the UI for till processing is done.
			//$.blockUI();
			$('#curplugin_id').val( sendData.curplugin_id );
			$('#gallery_extra').val( sendData.gallery_extra );
			$('#gallery_id').val( sendData.gallery_id );
			$('#gallery_params').val( sendData.gallery_params );
			$('#gallery_structure').val( sendData.gallery_structure );
			$('#gallery_title').val( sendData.gallery_title );
			$('#gallery_uselarge').val( sendData.gallery_uselarge );
			$('#images').val( sendData.images );
			$('#plugin_id').val( sendData.plugin_id );
		});  //  end : $('#px_buildSubmit').click
	});
	
	
	
	
	
	animateMe = function(what,where,who){
		setCurrentScreen(who);
	<?php if ( $px_controller->vars['use_effectSlide'] == 1 ) :?>
		$(what).animate({
			marginLeft : where
		}, 1000, 'easeInQuad');
	<?php else: ?>
		$(what).css({marginLeft:where});
	<?php endif; ?>
	};
	
	goBackTo = function(to){
		$('input[name=px_goBackToHidden]').val($('input[name=px_currentScreen]').val());
	};
	
	setCurrentScreen = function(current){
		goBackTo();
		$('input[name=px_currentScreen]').val(current);
	};
})(pxjQuery);